package com.hongtech.modules.knowledge.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.page.PageData;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.knowledge.dto.DrugInfoDTO;
import com.hongtech.modules.knowledge.entity.DrugInfoEntity;
import com.hongtech.modules.knowledge.service.DrugFdaService;
import com.hongtech.modules.knowledge.service.DrugInfoService;
import java.util.Map;

import com.hongtech.modules.knowledge.service.DrugTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 药品知识表
 *
 * @author admin 123456789@qq.com
 * @since 1.0.0 2023-09-01
 */
@Controller
@RequestMapping("/knowledge/druginfo")
public class DrugInfoController extends BaseController {

    @Autowired
    private DrugInfoService drugInfoService;

    @Autowired
    private DrugFdaService drugFdaService;

    @Autowired
    private DrugTypeService drugTypeService;

    @Permission("knowledge:druginfo:list")
    @RequestMapping("/list")
    public String list() {
        return "/knowledge/druginfo/list";
    }

    @Permission("knowledge:druginfo:list")
    @RequestMapping("/page")
    @ResponseBody
    public AjaxResultVo page(Integer offset, Integer limit) {
        LambdaQueryWrapper<DrugInfoEntity> ew = new LambdaQueryWrapper<>();
        Map<String, Object> pageParams = getPageParam(limit, offset);
        ew.orderByDesc(DrugInfoEntity::getCreateTime);
        PageData<DrugInfoDTO> pages = drugInfoService.pages(pageParams, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @Permission("knowledge:druginfo:save")
    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("drugTypeList", drugTypeService.list());
        model.addAttribute("drugFdaList", drugFdaService.list());
        return "/knowledge/druginfo/add";
    }

    @Permission("knowledge:druginfo:save")
    @OperateLog("创建药品知识表")
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResultVo doAdd(DrugInfoDTO dto) {
        drugInfoService.add(dto);
        return AjaxResultVo.success();
    }

    @Permission("knowledge:druginfo:edit")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        DrugInfoDTO dto = drugInfoService.get(id);
        model.addAttribute("entity", dto);
        model.addAttribute("drugTypeList", drugTypeService.list());
        model.addAttribute("drugFdaList", drugFdaService.list());
        return "/knowledge/druginfo/edit";
    }

    @Permission("knowledge:druginfo:edit")
    @OperateLog("编辑药品知识表")
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResultVo doEdit(DrugInfoDTO dto) {
        drugInfoService.update(dto);
        return AjaxResultVo.success();
    }

    @Permission("knowledge:druginfo:remove")
    @OperateLog("删除药品知识表")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResultVo delete(Long id) {
        drugInfoService.removeById(id);
        return AjaxResultVo.success();
    }

}
